package com.greenhouse9.prog05;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;

import com.greenhouse9.prog05.mypackage.Model01;


import java.util.HashMap;
import java.util.Enumeration;

import java.io.PrintWriter;

public class Controller extends HttpServlet {
	public void doPost( HttpServletRequest req,
		HttpServletResponse res)
	throws ServletException, IOException {
		req.setCharacterEncoding("Shift_JIS");
		res.setContentType("text/html; charset=Shift_JIS");
		PrintWriter out = res.getWriter();

		String arg01 = null;
		arg01 = req.getParameter("arg01");
		String page  = req.getParameter("page");

System.out.println(arg01);
        if(arg01.equals("value01")) {

    		HashMap<String, String> httpParams = new HashMap<String, String>();

			Enumeration  paramNames = req.getParameterNames();
			ServletContext context00 = getServletContext();
			System.out.println("C: output test");
			context00.log("output test");

			String paramName;
			String paramValue;

			while(paramNames.hasMoreElements()){
				paramName = (String)paramNames.nextElement();
				paramValue = (String)req.getParameter(paramName).replaceAll("\\$amp;","&");
				httpParams.put(paramName, paramValue);
			}

			System.out.println(httpParams.get("value01"));
			System.out.println(httpParams.get("value02"));

			Model01 model01 = null;
			try {
				model01 = (Model01)java.beans.Beans.instantiate(
					this.getClass().getClassLoader(),
					"mypackage.Model01");
				model01.setValue01( req.getParameter("value01"));
				model01.setValue02( req.getParameter("value02"));
				model01.kakezan();
				model01.setArg01( arg01);
				model01.setHttpParams(httpParams);
				model01.createEntity();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}

			req.setAttribute("mybean",model01);

			String target = page;
			ServletContext context = getServletContext();
			RequestDispatcher rd =
			    context.getRequestDispatcher(target);
			rd.forward(req,res);
		}
        else if (arg01.equals("value02")) {
			String target = page;
			ServletContext context = getServletContext();
			RequestDispatcher rd =
			    context.getRequestDispatcher(target);
			rd.forward(req,res);
        }
    }
}